package org.example.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.example.enums.BizCodeEnum;
import org.example.request.UserLoginRequest;
import org.example.request.UserRegisterRequest;
import org.example.service.FileService;
import org.example.service.UserService;
import org.example.util.CommonUtil;
import org.example.util.JsonData;
import org.example.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.spring.web.json.Json;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author LinN
 * @since 2022-09-25
 */
@RestController
@RequestMapping("/api/user/v1")
@Api(tags = "用户模块")
public class UserController {

    @Autowired
    private FileService fileService;

    @Autowired
    private UserService userService;

    @ApiOperation("用户头像上传")
    @PostMapping("/upload")
    public JsonData uploadUserImg(@ApiParam(value = "文件上传", required = true)
                                  @RequestPart("file") MultipartFile file) {
        String imageUrl = fileService.uploadUserImage(file);
        return imageUrl != null ? JsonData.buildSuccess(imageUrl) : JsonData.buildResult(BizCodeEnum.UPLOAD_FILE_ERROR);
    }

    @ApiOperation("用户注册")
    @PostMapping("/register")
    public JsonData userRegister(@ApiParam("用户注册对象") @RequestBody UserRegisterRequest userRegisterRequest) {
        JsonData jsonData = userService.register(userRegisterRequest);
        return jsonData;
    }

    @ApiOperation("用户登录")
    @PostMapping("/login")
    public JsonData userLogin(@ApiParam("用户登录对象") @RequestBody UserLoginRequest loginRequest) {
        JsonData jsonData = userService.userLogin(loginRequest);
        return jsonData;
    }

    @ApiOperation("用户个人信息查询")
    @GetMapping("detail")
    public JsonData detail() {
        UserVO userVO = userService.findUserDetail();
        return JsonData.buildSuccess(userVO);
    }
}

